const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const connect = require("gulp-connect");
const sourcemaps = require("gulp-sourcemaps");

gulp.task("copyHtml", done => {
  gulp.src("page/*.html").pipe(gulp.dest("dist/page")).pipe(connect.reload());
  done();
})

gulp.task("copyJs", done => {
  gulp.src("js/*.js").pipe(gulp.dest("dist/js")).pipe(connect.reload());
  done();
})

gulp.task("copyCss", done => {
  gulp.src("css/*.css").pipe(gulp.dest("dist/css")).pipe(connect.reload());
  done();
})


gulp.task("copyImg", done => {
  gulp.src("img/**/*").pipe(gulp.dest("dist/img"));
  done();
})

gulp.task("copyFrons", done => {
  gulp.src("frons/**/*").pipe(gulp.dest("dist/frons"));
  done();
})
gulp.task("copyLibs", done => {
  gulp.src("libs/**/*").pipe(gulp.dest("dist/libs"));
  done();
})

gulp.task("sass", done => {
  gulp.src("sass/*.scss")
    .pipe(sourcemaps.init())
    .pipe(sass({
      outputStyle: "expanded"
    }))
    .pipe(sourcemaps.write("./"))
    .pipe(gulp.dest("dist/css")).pipe(connect.reload());
  done();
})


gulp.task("watch", done => {
  gulp.watch("page/*.html", gulp.series("copyHtml"));
  gulp.watch("sass/*.scss", gulp.series("sass"));
  gulp.watch("css/*.css", gulp.series("copyCss"));
  gulp.watch("img/**/*", gulp.series("copyImg"));
  gulp.watch("js/*.js", gulp.series("copyJs"));
  gulp.watch("frons/**/*", gulp.series("copyFrons"));
  gulp.watch("libs/**/*", gulp.series("copyLibs"));
  done();
})

gulp.task("server", done => {
  connect.server({
    root: "dist",
    livereload: true
  })
  done();
});

gulp.task("default", gulp.series("server", "watch"));